﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Server
{
   public class CommuWarner
    {
       public  static CommuWarner Instance = new CommuWarner();
       Dictionary<string, DateTime> dic;
       QunYing.BLL.ICSA_Device bll_icsa = new QunYing.BLL.ICSA_Device();
       QunYing.BLL.DeviceRealTimeInfo bll_warnInfo = new QunYing.BLL.DeviceRealTimeInfo();

       private System.Timers.Timer timer = new System.Timers.Timer();
       //QunYing.BLL.DeviceInfo bll_DeviceInfo = new QunYing.BLL.DeviceInfo();

       public CommuWarner()
       {
           dic = new Dictionary<string, DateTime>();
           timer.AutoReset = false;
           timer.Interval = 1000 * 60 * 3;
           timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
           this.timer.Start();
           QunYing.Common.WarnInfoSeveice.Instance.WarnInfoReceived += new Action<string, int>(Instance_WarnInfoReceived);
       }

       void Instance_WarnInfoReceived(string arg1, int arg2)
       {
           try
           {
               this.Reset(bll_warnInfo.GetModel(arg2).发送号码);
           }
           catch (Exception ex)
           {
               QunYing.Common.Loger.Log.Info("重置通信故障状态出现异常：" + ex.Message);
           }
           //throw new NotImplementedException();
       }

       void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
       {
           this.timer.Stop();
           this.Process();
           this.timer.Start();
       }

       public void Reset(string sendTele)
       {
           if (this.dic.Keys.Contains(sendTele))
           {
               dic[sendTele] = DateTime.Now;
           }
           else
           {
               this.dic.Add(sendTele, DateTime.Now);
           }
       }

       private void Process()
       {
           foreach (var sendTele in this.dic.Keys.ToArray())
           {
               try
               {
                   if ((DateTime.Now - dic[sendTele]).TotalMinutes > 36 * 60)
                   {
                       dic[sendTele] = DateTime.Now;
                       QunYing.Model.DeviceRealTimeInfo drt = new QunYing.Model.DeviceRealTimeInfo();
                       drt.ORP告警 = false;
                       drt.PH告警 = false;
                       drt.传感器告警 = false;
                       drt.发送号码 = sendTele;
                       drt.反硝器告警 = false;
                       drt.钙反应器PH值 = 0;
                       drt.钙反应器告警 = false;
                       drt.告警处理状态 = "未处理";
                       drt.告警类型 = "通信故障";
                       drt.告警是否解除 = false;
                       drt.设备ID = sendTele;
                       drt.数据发送时间 = DateTime.Now;
                       drt.数据接收时间 = DateTime.Now;
                       drt.水位告警 = false;
                       drt.水温告警 = false;
                       drt.水循环告警 = false;
                       drt.水族箱ORP值 = 0;
                       drt.水族箱PH值 = 0;
                       drt.水族箱温度值 = 0;
                       drt.停电告警 = false;
                       drt.温控设备告警 = false;
                       drt.蓄电池电压 = 0;
                       drt.蓄电池告警 = false;
                       drt.氧循环告警 = false;
                       var vvv = bll_icsa.GetModel(sendTele);
                       drt.用户号码 = vvv.报警接收手机号码;
                       int dataID = bll_warnInfo.Add(drt);
                       QunYing.Common.WarnInfoSeveice.Instance.ProcessWarnInfo(vvv.所属经销商, dataID);
                   }
               }
               catch (Exception ex)
               {
                   QunYing.Common.Loger.Log.Error("监测通信故障时发生了异常：" +ex.Message);
               }
           }
       }
    }
}
